package com.qipay.sp.excutor.alipay;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.qipay.baas.model.Order;
import com.qipay.scalar.type.ApiTypeEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Component
public class AlipayWapExcutor extends AbstractExcutor {

    private final static String CONFIG_APPID_NAME="appid";
    private final static String CONFIG_ALIPAY_PUBLIC_NAME="rsa_public_key_alipay";
    private final static String CONFIG_RSA_PRIVATE_NAME="rsa_private_key";
    private final static String API_PARAM_OUT_TRADE_NO="out_trade_no";
    private final static String API_PARAM_RETURN_URL="return_url";
    private final static String API_PARAM_NOTIFY_URL="notify_url";
    private final static String API_PARAM_TOTAL_AMOUNT="total_amount";
    private final static String API_PARAM_SUBJECT="suject";
    private final static String API_URL="https://openapi.alipay.com/gateway.do";

    @Override
    protected ApiTypeEnum getApiTypeEnum() {
        return ApiTypeEnum.ALIPAY_WAP;
    }

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put(AlipayWapExcutor.API_PARAM_OUT_TRADE_NO, order.getTradeNo());
        apiParams.put(AlipayWapExcutor.API_PARAM_SUBJECT, order.getBody());
        apiParams.put(AlipayWapExcutor.API_PARAM_TOTAL_AMOUNT, order.getActualPayAmount());

        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {

        Object appId = getVal(spconfig, AlipayWapExcutor.CONFIG_APPID_NAME);
        Object rsaPrivateKey = getVal(spconfig, AlipayWapExcutor.CONFIG_RSA_PRIVATE_NAME);
        Object rsaPublicKeyAlipay = getVal(spconfig, AlipayWapExcutor.CONFIG_ALIPAY_PUBLIC_NAME);
        Object returnUrl = getVal(params, AlipayWapExcutor.API_PARAM_RETURN_URL);
        Object notifyUrl = getVal(params, AlipayWapExcutor.API_PARAM_NOTIFY_URL);
        Object outTradeNo = getVal(params, AlipayWapExcutor.API_PARAM_OUT_TRADE_NO);
        Object totalAmount = getVal(params, AlipayWapExcutor.API_PARAM_TOTAL_AMOUNT);
        Object subject = getVal(params, AlipayWapExcutor.API_PARAM_SUBJECT);

        logger.info("appId="+appId);
        logger.info("rsaPrivateKey="+rsaPrivateKey);
        logger.info("rsaPublicKeyAlipay="+rsaPublicKeyAlipay);

        AlipayClient alipayClient = new DefaultAlipayClient(AlipayWapExcutor.API_URL, appId.toString(), rsaPrivateKey.toString(), "json", "UTF-8", rsaPublicKeyAlipay.toString(), "RSA2"); // 获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();// 创建API对应的request
        alipayRequest.setReturnUrl(returnUrl.toString());
        alipayRequest.setNotifyUrl(notifyUrl.toString());// 在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" + " \"out_trade_no\":\"" + outTradeNo.toString() + "\","
                + " \"total_amount\":\"" + new BigDecimal((Integer) totalAmount).divide(new BigDecimal(100)) + "\","
                + " \"subject\":\""+subject.toString()+"\"," + " \"product_code\":\"QUICK_WAP_PAY\"" + " }");// 填充业务参数

        AlipayTradeWapPayResponse response = alipayClient.pageExecute(alipayRequest);
        Map<String, Object> resultMap = new HashMap<>();
        if(response.isSuccess()){
            resultMap.put("result_code", "0");
            resultMap.put("pay_info", response.getBody());
        } else {
            logger.info("支付宝wap接口请求失败："+response.getMsg());
            resultMap.put("result_code", "1");
            resultMap.put("message", "支付宝wap接口请求失败");
        }
        return resultMap;
    }
}
